using System;
using System.Windows.Input;

namespace Common.Command
{
	public class DelegateCommand : ICommand
	{
		private Func<object, bool> canExecute;

		private Action<object> executeAction;

		private bool canExecuteCache;

		public event EventHandler CanExecuteChanged;

		public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecute)
		{
			this.executeAction = executeAction;
			this.canExecute = canExecute;
		}

		public bool CanExecute(object parameter)
		{
			bool flag = canExecute(parameter);
			if (canExecuteCache != flag)
			{
				canExecuteCache = flag;
				RaiseCanExecuteChanged();
			}
			return canExecuteCache;
		}

		public void RaiseCanExecuteChanged()
		{
			if (this.CanExecuteChanged != null)
			{
				this.CanExecuteChanged(this, EventArgs.Empty);
			}
		}

		public void Execute(object parameter)
		{
			executeAction(parameter);
		}
	}
}
